package com.example.shopp_2301.category

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.base.mvvm.BaseFragment
import com.example.shopp_2301.R
import com.example.shopp_2301.adapter.GoodsAdapter
import com.example.shopp_2301.adapter.OneAdapter
import com.example.shopp_2301.adapter.TwoAdapter
import com.example.shopp_2301.databinding.FragmentCategoryBinding
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener


class CategoryFragment : BaseFragment<FragmentCategoryBinding,CategoryViewModel>() {

    lateinit var oneAdapter: OneAdapter
    lateinit var twoAdapter: TwoAdapter
    lateinit var goodsAdapter: GoodsAdapter

    var cid = 534
    var page = 1
    val limit = 3
    override fun initView() {
        //三个rv初始化
        oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(context)

        twoAdapter = TwoAdapter()
        binding.rvTwo.adapter = twoAdapter
        binding.rvTwo.layoutManager = GridLayoutManager(context,3)

        goodsAdapter = GoodsAdapter()
        binding.rvGoods.adapter = goodsAdapter
        binding.rvGoods.layoutManager = GridLayoutManager(context,2)

        //请求 ---一级分类和二级分类     商品列表
        viewModel.category()
        viewModel.categoryGoods(cid, limit, page)

        //点击一级分类 //点击的这个一级分类文字颜色发生变化 //二级数据发生变化  //三级数据也要发生变化
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            //一级分类文字颜色发生变化
            oneAdapter.setType(oneAdapter.data[position].id)
            //级数据发生变化
            twoAdapter.data.clear()
            twoAdapter.data.addAll(oneAdapter.data[position].child)
            twoAdapter.notifyDataSetChanged()
            twoAdapter.setId(twoAdapter.data[0].id)
            //三级数据也要发生变化===展示的是二级分类的第一个
            cid = twoAdapter.data[0].id
            page = 1//分类发生变化后，页码从1开始
            viewModel.categoryGoods(cid, limit, page)
        }
        //点击二级分类  //当前分类的文字颜色发生变化  //三级数据发生变化
        twoAdapter.setOnItemClickListener { adapter, view, position ->
            twoAdapter.setId(twoAdapter.data[position].id)

            cid = twoAdapter.data[position].id
            page = 1
            viewModel.categoryGoods(cid, limit, page)
        }

        binding.smart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.categoryGoods(cid, limit, page)
                binding.smart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.categoryGoods(cid, limit, page)
                binding.smart.finishLoadMore()
            }
        })


    }

    override fun initData() {
        viewModel.cateSuccess.observe(this){
            if(it.code==200){
                //显示一级分类
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                //显示二级分类  ---二级分类应该显示的是 一级分类中第一个的child

                twoAdapter.data.clear()
                twoAdapter.data.addAll(oneAdapter.data[0].child)
                twoAdapter.notifyDataSetChanged()

            }
        }
        viewModel.goodsSuccess.observe(this){
            if(it.code==200){
                if(page==1){
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<CategoryViewModel> {
        return CategoryViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.fragment_category
    }

}